package com.ruoyi.system.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDomainName;
import com.ruoyi.system.service.ISysDomainNameService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 域名Controller
 *
 * @author ruoyi
 * @date 2024-11-29
 */
@RestController
@RequestMapping("/system/domain_name")
public class SysDomainNameController extends BaseController
{
    @Autowired
    private ISysDomainNameService sysDomainNameService;

    /**
     * 查询域名列表
     */
    @PreAuthorize("@ss.hasPermi('system:domain_name:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDomainName sysDomainName)
    {
        startPage();
        List<SysDomainName> list = sysDomainNameService.selectSysDomainNameList(sysDomainName);
        return getDataTable(list);
    }

    /**
     * 导出域名列表
     */
    @PreAuthorize("@ss.hasPermi('system:domain_name:export')")
    @Log(title = "域名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDomainName sysDomainName)
    {
        List<SysDomainName> list = sysDomainNameService.selectSysDomainNameList(sysDomainName);
        ExcelUtil<SysDomainName> util = new ExcelUtil<SysDomainName>(SysDomainName.class);
        util.exportExcel(response, list, "域名数据");
    }

    /**
     * 导入违规关键词
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysDomainName> util = new ExcelUtil<SysDomainName>(SysDomainName.class);
        List<SysDomainName> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysDomainNameService.importUser(userList, updateSupport, operName);
        return success(message);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysDomainName> util = new ExcelUtil<SysDomainName>(SysDomainName.class);
        util.importTemplateExcel(response, "关键词数据");
    }

    /**
     * 获取域名详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:domain_name:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysDomainNameService.getById(id));
    }

    /**
     * 新增域名
     */
    @PreAuthorize("@ss.hasPermi('system:domain_name:add')")
    @Log(title = "域名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDomainName sysDomainName) {
        return toAjax(sysDomainNameService.save(sysDomainName));
    }

    /**
     * 修改域名
     */
    @PreAuthorize("@ss.hasPermi('system:domain_name:edit')")
    @Log(title = "域名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDomainName sysDomainName) {
        return toAjax(sysDomainNameService.updateById(sysDomainName));
    }

    /**
     * 删除域名
     */
    @PreAuthorize("@ss.hasPermi('system:domain_name:remove')")
    @Log(title = "域名", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysDomainNameService.removeByIds(Arrays.asList(ids)));
    }
}
